{extend name="base/base" /}
{block name="style"}
<style type="text/css">

</style>
{/block}
{block name="content"}
  <div class="layui-form-item layui-form-pane">
      <div class="layui-inline">
        <label class="layui-form-label">状态</label>
        <div class="layui-input-inline">
          <select class="layui-input" name='status' id='status'>
            <option value="">全部</option>
            <option value="1">等待领取</option>
            <option value="2">已领取待发放</option>
            <option value="3">已发放</option>

          </select>
        </div>
      </div>
      <div class="layui-inline">
          <label class="layui-form-label">用户ID</label>
          <div class="layui-input-inline">
              <input type="text" name="uid" autocomplete="off" class="layui-input" id='uid'>
          </div>
      </div>
      <div class="layui-inline">
          <label class="layui-form-label">团队UID</label>
          <div class="layui-input-inline">
              <input type="text" name="uuid" autocomplete="off" class="layui-input" id='uuid'>
          </div>
      </div>
      <div class="layui-inline">
          <label class="layui-form-label">用户名</label>
          <div class="layui-input-inline">
              <input type="text" name="username" autocomplete="off" class="layui-input" id='username'>
          </div>
      </div>
      <div class="layui-inline">
          <label class="layui-form-label">红包码</label>
          <div class="layui-input-inline">
              <input type="text" name="code" autocomplete="off" class="layui-input" id='code'>
          </div>
      </div>
        <div class="layui-inline">
          <label class="layui-form-label">红包领取码</label>
          <div class="layui-input-inline">
              <input type="text" name="hb_code" autocomplete="off" class="layui-input" id='hb_code'>
          </div>
      </div>
      <div class="layui-inline">
        <div class="demoTable">
          <button class="layui-btn" data-type="reload">搜索</button>
          <button class="layui-btn layui-btn-danger" onclick="add()">发起申请</button>
        </div>
      </div>
        <script type="text/html" id="edit">
    <a class="layui-btn layui-btn-xs" lay-event="edit">发放</a>
    <a class="layui-btn layui-btn-xs" lay-event="del">删除</a>
  </script>
  <table id="table" lay-filter="table"></table> 
  <script type="text/html" id="toolbarDemo">
  <div class="layui-btn-container">
    <button class="layui-btn layui-btn-sm" lay-event="add">一键发放</button>
  </div>
</script>
</div>
{/block}
{block name="script"}
<script>
layui.use(['table', 'laydate'], function(){
  var table = layui.table;
  var laydate = layui.laydate;
  table.render({
    elem: '#table'
    ,height: 'full-200'
    ,defaultToolbar: ['filter']
    ,toolbar: '#toolbarDemo'
    ,url: '/admin/reward/hbList/?type=4' //数据接口
    ,limit:20
    ,page: true //开启分页
    ,id:'reload'
    ,cols: [[ //表头
        {type: 'checkbox', fixed: 'left'}
        ,{field:'uid', title: '用户ID', width: 100}
        ,{field:'username', title: '用户名', width: 150}
        ,{field:'code', title: '红包码', width: 150}
        ,{field:'hb_code', title: '红包领取码', width: 150}
        ,{field:'total_amount', title: '红包总金额', width: 150}
        ,{field:'num', title: '红包个数', width: 150}
        ,{field:'amount', title: '当前红包金额', width: 150}
        ,{field:'create_date', title: '申请时间', width: 200}
        // ,{field:'use_date', title: '使用时间', width: 200}
        // ,{field:'type', title: '类型', width: 200,templet: function(d){
        //   if (d.type == 11) {
        //     return '<span style="color:blue">合伙人</span>'
        //   }
        //   if (d.type == 12) {
        //     return '<span style="color:red">营销经理</span>'
        //   }
        // }}
        ,{field:'status', title: '状态', width: 150,templet: function(d){

          if (d.status == 1) {
            return '<span style="color:blue">等待领取</span>'
          }
          if (d.status == 2) {
            return '<span style="color:red">已领取待发放</span>'
          }
          if (d.status == 3) {
            return '<span style="color:blue">已发放</span>'
          }
          if (d.status == 4) {
            return '<span style="color:green">已拒绝</span>'
          }
        }}
        ,{field:'adminname', title: '发起人', width: 150}
        
        ,{title: '操作', toolbar: '#edit', width: 200}
    ]]
  });
   //监听表格复选框选择
  table.on('checkbox(table)', function(obj){
   //console.log(obj)
  });
  //监听事件
table.on('toolbar(table)', function(obj){
  var checkStatus = table.checkStatus(obj.config.id);
  switch(obj.event){
    case 'add':
        var da = checkStatus.data;
        if (da.length == 0) {
          layer.msg('未选择记录');
          return;
        }
        layer.confirm('确定发放？', {
          btn: ['确定'] //可以无限个按钮
          ,btn3: function(index, layero){
            //按钮【按钮三】的回调
            layer.close(index);
          }
        }, function(index, layero){
          //按钮【按钮一】的回调
          // var value = layui.jquery('#layui-layer'+index + " .layui-layer-input").val();
          var pa = '';
          for (var i = 0; i < da.length; i++) {
            var id = da[i].id;
            pa = pa + ',' + id;
            // console.log();
            // pa.push({id:da[i].id});
          }
          $.ajax({
              url:"{:url('admin/reward/ffhb2')}",
              type:"POST",
              // data:{data:JSON.stringify(pa)},
              data:{data:pa},

              success:function(ret){
                  if (ret.code == 0) {
                    layer.msg('操作成功');
                    $(".layui-laypage-btn")[0].click();
                    layer.close(index);
                  }
                  if (ret.code == 1) {
                      layer.msg(ret.msg);
                  }
              }
          });
          //按钮【按钮一】的回调
        }, function(index){
          //按钮【按钮二】的回调
          //按钮【按钮二】的回调
        });
    break;
  };
});
  

  table.on('tool(table)', function(obj){
      var data = obj.data;

     if(obj.event === 'edit'){
       layer.confirm('确定发放？', {
          btn: ['确定'] //可以无限个按钮
          ,btn3: function(index, layero){
            //按钮【按钮三】的回调
            layer.close(index);
          }
        }, function(index, layero){
          //按钮【按钮一】的回调
          $.ajax({
              url:"{:url('admin/reward/ffhb')}",
              type:"POST",
              data:{id:data.id},
              success:function(ret){
                  if (ret.code == 0) {
                    layer.msg('操作成功');
                    $(".layui-laypage-btn")[0].click();
                    layer.close(index);
                  }
                  if (ret.code == 1) {
                      layer.msg(ret.msg);
                  }
              }
          });
          //按钮【按钮一】的回调
        }, function(index){
          //按钮【按钮二】的回调
          //按钮【按钮二】的回调
        });
      }
      if(obj.event === 'del'){
       layer.confirm('确定删除？', {
          btn: ['确定'] //可以无限个按钮
          ,btn3: function(index, layero){
            //按钮【按钮三】的回调
            layer.close(index);
          }
        }, function(index, layero){
          //按钮【按钮一】的回调
          $.ajax({
              url:"{:url('admin/reward/delhb')}",
              type:"POST",
              data:{id:data.id},
              success:function(ret){
                  if (ret.code == 0) {
                    layer.msg('操作成功');
                    $(".layui-laypage-btn")[0].click();
                    layer.close(index);
                  }
                  if (ret.code == 1) {
                      layer.msg(ret.msg);
                  }
              }
          });
          //按钮【按钮一】的回调
        }, function(index){
          //按钮【按钮二】的回调
          //按钮【按钮二】的回调
        });
      }
    });
  // 搜索
  var $ = layui.$, active = {
        reload: function(){
            table.reload('reload', {
                where: {
                    username: $('#username').val(),
                    uid: $('#uid').val(),
                    status: $('#status').val(),
                    type: $('#type').val(),
                    code: $('#code').val(),
                    hb_code: $('#hb_code').val(),
                    // is_active: $('#is_active').val(),
                    phone:$('#phone').val(),
                    uuid:$('#uuid').val()

                }
            });
        }
    };
    $('.demoTable .layui-btn').on('click', function(){
    var type = $(this).data('type');
    active[type] ? active[type].call(this) : '';

  });

  //日期
  laydate.render({
    elem: '#start_time'
  });
  laydate.render({
    elem: '#end_time'
  });
});
function add(){
  layer.open({
        type: 2 ,
        title: '发起申请',   //标题
        area: ['500px', '500px'],   //宽高
        shade: 0.4,   //遮罩透明度
        content: '/admin/reward/hb2'
        // end:function(){
        //   $(".layui-laypage-btn")[0].click();
        // }
        });
}
</script>
{/block}
